package com.learn.xml.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public abstract class ReadXmlUtil {

    private final static Logger log = LoggerFactory.getLogger(ReadXmlUtil.class);
    public ReadXmlUtil() {
    }

    public Element readXml(InputStream var1) throws Exception {
        SAXReader var2 = new SAXReader();
        Object var3 = null;
        Element var4 = null;

        try {
            Document var5 = var2.read(var1);
            var4 = var5.getRootElement();
            return var4;
        } catch (Exception var6) {
            log.error(var6.getMessage());
            throw new Exception(var6.getMessage());
        }
    }

    public LinkedHashMap<String, Object> getXmlContent(Iterator var1) throws Exception {
        Element var2 = null;
        LinkedHashMap var3 = null;
        LinkedHashMap var4 = null;
        LinkedHashMap var5 = new LinkedHashMap();

        try {
            if (var1 != null && var1.hasNext()) {
                while(true) {
                    while(var1.hasNext()) {
                        var2 = (Element)var1.next();
                        if (var2.nodeCount() > 0) {
                            var4 = this.getXmlContent(var2.elementIterator());
                            if (var2.attribute("name") != null) {
                                var5.put(var2.attribute("name").getData().toString(), var4);
                            } else {
                                var5.put(var2.getName(), var4);
                            }
                        } else {
                            var3 = this.parseXmlSpecial(var2);
                            Iterator var6 = var3.entrySet().iterator();

                            while(var6.hasNext()) {
                                Map.Entry var7 = (Map.Entry)var6.next();
                                var5.put(var7.getKey(), var7.getValue());
                            }
                        }
                    }

                    return var5;
                }
            } else {
                return var5;
            }
        } catch (Exception var8) {
            log.error(var8.getMessage());
            throw new Exception(var8.getMessage());
        }
    }

    public abstract LinkedHashMap<String, Object> parseXmlSpecial(Element var1) throws Exception;
}

